package com.onlinelearn.paymentservice.service;

import com.onlinelearn.paymentservice.model.PaymentOrder;
import com.onlinelearn.paymentservice.model.PaymentMethod;
import com.onlinelearn.paymentservice.exception.PaymentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

@Service
public class PaymentGatewayService {
    private static final Logger logger = LoggerFactory.getLogger(PaymentGatewayService.class);

    public PaymentResult processPayment(PaymentOrder order, PaymentMethod method) {
        try {
            // 模拟第三方支付服务
            switch (method) {
                case WECHAT_PAY:
                    return processWechatPay(order);
                case ALIPAY:
                    return processAlipay(order);
                case BANK_CARD:
                    return processBankCardPay(order);
                default:
                    throw new PaymentException(
                        "不支持的支付方式", 
                        PaymentException.ErrorCode.PAYMENT_GATEWAY_ERROR
                    );
            }
        } catch (Exception e) {
            logger.error("支付处理失败: {}", e.getMessage(), e);
            throw new PaymentException(
                "支付处理失败: " + e.getMessage(), 
                PaymentException.ErrorCode.PAYMENT_GATEWAY_ERROR
            );
        }
    }

    private PaymentResult processWechatPay(PaymentOrder order) {
        // 微信支付逻辑
        if (order.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new PaymentException(
                "无效的支付金额", 
                PaymentException.ErrorCode.INSUFFICIENT_BALANCE
            );
        }
        return PaymentResult.success(generateTransactionId(), order.getAmount());
    }

    private PaymentResult processAlipay(PaymentOrder order) {
        // 支付宝支付逻辑
        if (order.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new PaymentException(
                "无效的支付金额", 
                PaymentException.ErrorCode.INSUFFICIENT_BALANCE
            );
        }
        return PaymentResult.success(generateTransactionId(), order.getAmount());
    }

    private PaymentResult processBankCardPay(PaymentOrder order) {
        // 银行卡支付逻辑
        if (order.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new PaymentException(
                "无效的支付金额", 
                PaymentException.ErrorCode.INSUFFICIENT_BALANCE
            );
        }
        return PaymentResult.success(generateTransactionId(), order.getAmount());
    }

    private String generateTransactionId() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}

// 支付结果类
class PaymentResult {
    private boolean success;
    private String transactionId;
    private BigDecimal amount;
    private String errorMessage;

    private PaymentResult(boolean success, String transactionId, BigDecimal amount, String errorMessage) {
        this.success = success;
        this.transactionId = transactionId;
        this.amount = amount;
        this.errorMessage = errorMessage;
    }

    public static PaymentResult success(String transactionId, BigDecimal amount) {
        return new PaymentResult(true, transactionId, amount, null);
    }

    public static PaymentResult failed(String errorMessage) {
        return new PaymentResult(false, null, null, errorMessage);
    }

    // Getters
    public boolean isSuccess() {
        return success;
    }
} 